/*
 * Copyright (c) 2006-2021, Ulandlink Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-08-18     lfz         first version
 */
#include <bsp_system.h>
#include <rtthread.h>

#define _SCB_BASE       (0xE000E010UL)
#define _SYSTICK_CTRL   (*(uint32_t *)(_SCB_BASE + 0x0))
#define _SYSTICK_LOAD   (*(uint32_t *)(_SCB_BASE + 0x4))
#define _SYSTICK_VAL    (*(uint32_t *)(_SCB_BASE + 0x8))
#define _SYSTICK_CALIB  (*(uint32_t *)(_SCB_BASE + 0xC))
#define _SYSTICK_PRI    (*(uint8_t  *)(0xE000ED23UL))

void SysTick_Handler(void)
{
	/* enter interrupt */
	rt_interrupt_enter();

	rt_tick_increase();

	/* leave interrupt */
	rt_interrupt_leave();
}

uint32_t systick_config(uint32_t ticks)
{
	if ((ticks - 1) > 0xFFFFFF) {
			return 1;
	}
	_SYSTICK_LOAD = ticks - 1; 
	_SYSTICK_PRI  = 0xFF;
	_SYSTICK_VAL  = 0;
	_SYSTICK_CTRL = 0x07;	
	return 0;
}

uint32_t bsp_tick_get(void) 
{
	return (uint32_t)rt_tick_get();
}

bool bsp_tick_expire(uint32_t ustarttick, uint32_t utmot) 
{
	uint32_t tick = rt_tick_get();
	return ( (tick - ustarttick) > utmot);
}

/* USER CODE BEGIN 2 */

/* USER CODE END 2 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
